/*
 * Copyright 2017 Baiyyy
 * @(#)DeviceController.java
 */

package com.byyy.ccts.modules.cc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.byyy.ccts.core.common.controller.BaseCRUDController;
import com.byyy.ccts.core.model.PageJson;
import com.byyy.ccts.core.query.annotation.PageableDefaults;
import com.byyy.ccts.core.query.data.PropertyPreFilterable;
import com.byyy.ccts.core.query.data.Queryable;
import com.byyy.ccts.core.query.utils.QueryableConvertUtils;
import com.byyy.ccts.core.query.wrapper.EntityWrapper;
import com.byyy.ccts.core.security.shiro.authz.annotation.RequiresPathPermission;
import com.byyy.ccts.core.utils.CacheUtils;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.cc.Global;
import com.byyy.ccts.modules.cc.entity.ccts.TADevice;
import com.byyy.ccts.modules.cc.service.IDeviceOptCmdService;
import com.byyy.ccts.modules.cc.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 设备信息
 * Created by DELL on 2018-5-14.
 */
@Controller
@RequestMapping("${admin.url.prefix}/cc/device")
@RequiresPathPermission("cc:device")
public class DeviceController extends BaseCRUDController<TADevice, String> {

    /** 设备服务类 */
    @Autowired
    private IDeviceService deviceService;

    /** 设备命令服务类 */
    @Autowired
    private IDeviceOptCmdService deviceCmdService;

    /**
     * 在异步获取数据之前
     *
     * @param queryable
     * @param request
     * @param response
     */
    @Override
    public void preAjaxList(Queryable queryable, EntityWrapper<TADevice> entityWrapper, HttpServletRequest request, HttpServletResponse response) {
        entityWrapper.eq("SHOW", "1");
    }

    /**
     * 保存之前先进行判断，是否修改了设备参数，如果修改，则进行设备交互
     * @param entity
     * @param request
     * @param response
     */
    @Override
    public void preSave(TADevice entity, HttpServletRequest request, HttpServletResponse response) {
        this.deviceCmdService.changeDeviceParam(entity);
    }

    /**
     * 保存之前先进行判断，是否修改了设备参数，如果修改，则进行设备交互
     * @param entity
     * @param request
     * @param response
     */
    @Override
    public void afterSave(TADevice entity, HttpServletRequest request, HttpServletResponse response) {
        CacheUtils.put(Global.KEY_DEVICE_LIST, this.deviceService.selectList(null));
    }

    /**
     * 设备列表
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="deviceInfoList", method = RequestMethod.GET)
    public String deviceInfoList(Model model, HttpServletRequest request, HttpServletResponse response) {
        return display("../deviceinfo/list");
    }

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "deviceInfoAjaxList", method = { RequestMethod.GET, RequestMethod.POST })
    @PageableDefaults(sort = "id=desc")
    public void deviceAjaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                                HttpServletResponse response) throws IOException {
        EntityWrapper<TADevice> entityWrapper = new EntityWrapper<TADevice>(entityClass);
        preAjaxList(queryable,entityWrapper, request, response);
        propertyPreFilterable.addQueryProperty("id");
        // 预处理
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass);
        SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
        PageJson<TADevice> pagejson = new PageJson<TADevice>(this.deviceService.deviceInfoList(queryable, entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }

    /**
     * 温度列表
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="deviceHumitureList", method = RequestMethod.GET)
    public String deviceHumitureList(Model model, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("sn", request.getParameter("sn"));
        return display("../deviceinfo/humiture/deviceHumitureList");
    }

}
